package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.db.domain.MarketSearchHistoryExample;
import com.cskaoyan.market.db.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.market.service.AdminHistoryService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/24
 * Time: 21:18
 * Description:
 */
public class AdminHistoryServiceImpl implements AdminHistoryService {
    @Override
    public List<MarketSearchHistory> list(String page, String limit, String userId, String keyword, String sort, String order) {
        // 1. 获取sqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketSearchHistoryMapper mapper = session.getMapper(MarketSearchHistoryMapper.class);
        // 2.增加筛选条件
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        // userId非空时查询
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        // keyword非空时查询
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordEqualTo(keyword);
        }
        // 3. 增加排序方式
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        // 4. 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketSearchHistory> marketSearchHistories = mapper.selectByExample(marketSearchHistoryExample);
        // 5. 提交事务
        session.commit();
        session.close();
        // 6. 将List返回给Controller
        return marketSearchHistories;
    }
}
